package com.ycdr.leetcode.stack.example;

/**
 * 基于数组实现的Stack
 * @date 2020年05月31日
 * @author ycdr
 */
public class ArrayStack {

    public int top=0;
    public Object[] t=null;
    public ArrayStack(int i){
       this.t=new Object[i];
    }

    public void push(Object o){
        if (top==t.length-1){
            System.err.println("Stack is Has overflowed");
            return;
        }
        t[top++]=o;
    }

    public Object pop(){
        int temp=top;
        if (--temp==-1){
             System.err.println("Stack is Empty");
            return null;
        }
        return t[--top];
    }

    public Object peek(){
        int temp=top;
        if (temp==0){
            return t[temp];
        }
        return t[--temp];
    }


    public void print(){
        for (int i = 0; i < t.length; i++) {
            System.out.print(t[i]+" ");
        }
    }

    public static void main(String[] args) {
        ArrayStack stack=new ArrayStack(10);
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        stack.print();
        System.out.println();
        System.out.println("peek--"+stack.peek());

        System.out.println("pop--"+stack.pop());
        System.out.println("pop--"+stack.pop());
        System.out.println("pop--"+stack.pop());
        System.out.println("pop--"+stack.pop());
        System.out.println("pop--"+stack.pop());
        System.out.println("pop--"+stack.pop());
        System.out.println("peek--"+stack.peek());


    }
}
